package com.jiaoxiaobo.consumer;

import com.jiaoxiaobo.RabbitMQUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.GetResponse;

import java.util.HashMap;
import java.util.Map;

public class PullConsumer {

    private static final String QUEUE_NAME = "hello-class-queue";

    public static void main(String[] args) throws Exception {
        Connection connection = RabbitMQUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.basicQos(100);

        Map<String,Object> params = new HashMap<>();

        channel.queueDeclare(QUEUE_NAME, false, false, false, params);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
        GetResponse response = channel.basicGet(QUEUE_NAME, false);

        if(null != response){
            System.out.println(new String(response.getBody(),"UTF-8"));
        }

        Thread.sleep(10000);
        GetResponse response2 = channel.basicGet(QUEUE_NAME, false);
        if(null != response2){
            System.out.println(new String(response2.getBody(),"UTF-8"));
        }

        channel.close();
        connection.close();
    }

}
